//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
#include "vSModifierAdresse.h"
#pragma link "CComboBx"
#pragma link "CEdit"
//---------------------------------------------------------------------------
//              CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TvAModifierAdresse::TvAModifierAdresse(const AnsiString &asLogiciel,
                                                  const AnsiString &asVersion,
                                                  TStringList * lRefCode,
                                                  const int &nCount,
                                                  TComponent* Owner)
    : TForm(Owner)
{
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        //bbConsulter->Glyph->LoadFromResourceName((int)HInstance, "ID_ACTION_CONSULTER");
        
        m_nPageAttente = AUCUNE_SELECTION;
        m_nCount   = nCount;
        m_lRefCode = lRefCode;
        eLogiciel->Text = asLogiciel;
        eVersion->Text  = asVersion;

        if(m_nCount > 1) {
            lNbreAdresse->Visible = true;
            lNbreAdresse->Text = (AnsiString)"Nombre d'adresses sélectionnées: "+m_nCount;
        }

        GetAdresse(GetRefCode());

        if( m_asPageWeb != MULTIPLES ) {

            CComboAttente * Attente = NULL;
            Attente = new CComboAttente();
            Attente->Remplir(cbAttente->Items);
            if(Attente) {delete Attente; Attente = NULL;}
        
            cbAttente->ItemIndex = m_nPageAttente;

            pAttente->Visible = true;
        }

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              DESTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TvAModifierAdresse::~TvAModifierAdresse()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//          RECUPERATION DU REF_CODE
//---------------------------------------------------------------------------
AnsiString __fastcall TvAModifierAdresse::GetRefCode()
{
    AnsiString Chaine = "";
    try {
        bool bFlag = false;
        for(int i=0; i<m_lRefCode->Count; i++) {
            if(!bFlag) {
                Chaine += AnsiString(m_lRefCode->Strings[i]);
                bFlag = true;
            }else {
                Chaine += (AnsiString) ","+m_lRefCode->Strings[i].ToInt();
            }
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        Chaine = "";}
    return Chaine;
}
//---------------------------------------------------------------------------
//          RECUPERATION DE L ADRESSE POUR SELECTION MULTIPLE
//---------------------------------------------------------------------------
void __fastcall TvAModifierAdresse::GetAdresse(const AnsiString &asChaine)
{
    TStringList * Refer = NULL;
    TStringList * Desig = NULL;
    TStringList * Web   = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {

        bool bMultipleRef = false;
        bool bMultipleDes = false;
        bool bMultipleWeb = false;

        Refer = new TStringList();
        Desig = new TStringList();
        Web   = new TStringList();

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select ref_nom,"+          //0
                                                    "ref_sous_nom,"+    //1
                                                    "ref_adresse,"+     //2
                                                    "ref_attente "+      //3
                                            "from refer "+
                                            "where ref_code in ("+asChaine+")";
        MaRequete->Open();
        MaRequete->First();
        //Premier element
        AnsiString Reference    = asRequete(0);
        AnsiString Designation  = asRequete(1);
        AnsiString PageWeb      = asRequete(2);


        m_nPageAttente = nRequete(3);
        //element suivant
        while(!MaRequete->Eof){

            Refer->Add(asRequete(0) );
            Desig->Add(asRequete(1) );
            Web->Add(asRequete(2) );

            MaRequete->Next();
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();


        //Reference
        for(int i=0; i<Refer->Count; i++) {
            if(Reference != Refer->Strings[i]) {
                bMultipleRef = true;
                break;
            }
        }

        if(!bMultipleRef) {
            eRefer->Text = Reference;
        }else {
            eRefer->Text     = MULTIPLES;
            eRefer->ReadOnly = true;
            eRefer->Color    = clMenu;
        }

        //Designation
        for(int j=0; j<Desig->Count; j++) {
          if(Designation != Desig->Strings[j]) {
              bMultipleDes = true;
              break;
          }
        }

        if(!bMultipleDes) {
            eDesignation->Text     = Designation;
        }else {
            eDesignation->Text     = MULTIPLES;
            eDesignation->ReadOnly = true;
            eDesignation->Color    = clMenu;
        }

        //Page web
        for(int k=0; k<Web->Count; k++) {
          if(PageWeb != Web->Strings[k]) {
              bMultipleWeb = true;
              break;
          }
        }

        if(!bMultipleWeb) {
            mPageWeb->Text     = PageWeb;
        }else {
            mPageWeb->Text     = MULTIPLES;
            mPageWeb->ReadOnly = true;
            mPageWeb->Color    = clMenu;
        }

        m_asRefer       = eRefer->Text;
        m_asDesignation = eDesignation->Text;
        m_asPageWeb     = mPageWeb->Text;
        
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(Web)   {delete Web; Web = NULL;}
    if(Desig) {delete Desig; Desig = NULL;}
    if(Refer) {delete Refer; Refer = NULL;}
}
//---------------------------------------------------------------------------
//                  Valider la modification
//---------------------------------------------------------------------------
void __fastcall TvAModifierAdresse::bbOkClick(TObject *Sender)
{
    CHistorique * Hist = NULL;
    try {
        AnsiString asMessage = EmptyStr;
        if(    m_asRefer == eRefer->Text
            && m_asDesignation == eDesignation->Text
            && m_asPageWeb == mPageWeb->Text
            && m_nPageAttente == cbAttente->ItemIndex ) {
            MessageDlg("Veuillez modifier les champs.", mtInformation, TMsgDlgButtons() << mbOK, 0);
            ModalResult = mrNone;
        }else {
            if(   (eRefer->Text       == MULTIPLES)
                &&(eDesignation->Text == MULTIPLES)
                &&(mPageWeb->Text     == MULTIPLES)) {
                MessageDlg("Impossible de modifier ces adresses !!!", mtWarning, TMsgDlgButtons() << mbOK, 0);
            }else{
                if( (eRefer->Text != "\\") && (eDesignation->Text != "\\") && (mPageWeb->Text != "\\") ) {
                    if( !eRefer->Text.IsEmpty() && !eDesignation->Text.IsEmpty() && !mPageWeb->Text.IsEmpty() ) {
                        if(m_nCount> 1) {
                            asMessage.sprintf("Confirmez vous la modification des %d adresses sélectionnées ?", m_nCount);
                        }else {
                            asMessage = "Confirmez vous la modification de cette adresse ?";
                        }

                        if (MessageDlg(asMessage, mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes) {

                            Hist = new CHistorique();
                            for(int i=0; i<m_lRefCode->Count; i++) {
                                dmWebHelp->tRefer->Active = true;
                                dmWebHelp->tRefer->First();
                                while(!dmWebHelp->tRefer->Eof) {
                                    if(ntRefer(REF_CODE) == m_lRefCode->Strings[i].ToInt()) {
                                        dmWebHelp->tRefer->Edit();
                                        if(eRefer->Text != MULTIPLES)
                                            astRefer(REF_NOM) = eRefer->Text;

                                        if(eDesignation->Text != MULTIPLES)
                                            astRefer(REF_SOUS_NOM) = eDesignation->Text;

                                        if(mPageWeb->Text != MULTIPLES){
                                            astRefer(REF_ADRESSE) = mPageWeb->Text;
                                            ntRefer(REF_ATTENTE)  = cbAttente->ItemIndex;
                                        }
                                        dmWebHelp->tRefer->Post();
                                        Hist->AjouterOperation(Histo_MODIF_REF,
                                                                eLogiciel->Text,
                                                                eVersion->Text,
                                                                eRefer->Text,
                                                                eDesignation->Text,
                                                                mPageWeb->Text);
                                        break;
                                    }
                                    dmWebHelp->tRefer->Next();
                                }
                            }
                            dmWebHelp->trWebHelp->Commit();
                            ModalResult = mrOk;
                        }
                    }else {
                        MessageDlg("Veuillez remplir les champs.", mtInformation, TMsgDlgButtons() << mbOK, 0);
                        ModalResult = mrNone;
                    }
                }
            }
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(Hist) {delete Hist; Hist = NULL;}
}
//---------------------------------------------------------------------------
//                      Consulter la page Web
//---------------------------------------------------------------------------
void __fastcall TvAModifierAdresse::bbConsulterClick(TObject *Sender)
{

    CVerifInternet * VerifInternet = NULL;
    try {
        VerifInternet = new CVerifInternet();

        if( !Trim(mPageWeb->Text).IsEmpty() ) {
            if( mPageWeb->Text != MULTIPLES ) {
                if(VerifInternet->ConnexionAuSite(mPageWeb->Text))
                    ShellExecute(NULL, "Open", mPageWeb->Text.c_str(),NULL, NULL, SW_SHOWMAXIMIZED);
            }
        }else
            MessageDlg("La mise à jour de la page Web sélectionner n'a pas été effectuée.", mtInformation, TMsgDlgButtons() << mbOK, 0);

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(VerifInternet) {delete VerifInternet; VerifInternet = NULL;}

}
//---------------------------------------------------------------------------
void __fastcall TvAModifierAdresse::cbAttenteChange(TObject *Sender)
{
    try {
       // m_nPageAttente = cbAttente->ItemIndex;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
void __fastcall TvAModifierAdresse::mPageWebChange(TObject *Sender)
{
    try {
        int idx = cbAttente->ItemIndex;
        cbAttente->ItemIndex = AUCUNE_SELECTION;
        if(mPageWeb->Text.IsEmpty())
            cbAttente->ItemIndex = idx;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------

